<?php

class Comment {
	private $ID;
	private $Author;
	private $AuthorEmail;
	private $AuthorWebSite;
	private $DateTime;
	private $CommentContent;
	
	private $ApplicationOwner;
	private $ApplicationEntryID;
	
	private $Controller;
	
	function __construct($comment_id = null, CommentController $Controller){
		if($comment_id == null){
			return false;
		}
		
		$this->Controller = $Controller;
		$MySQL = $Controller->UseApps["mysql"];
		
		$CommentQuery = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}comment WHERE id = $comment_id");
		
		if(mysql_num_rows($CommentQuery) <= 0){
			return false;
		}
		
		$CommentData = mysql_fetch_assoc($CommentQuery);
		
		$this->ID = $CommentData["id"];
		$this->AuthorEmail = $CommentData["author_email"];
		$this->AuthorWebSite = $CommentData["author_website"];
		$this->DateTime = $CommentData["date_time"];
		$this->ApplicationOwner = $CommentData["application_owner"];
		$this->ApplicationEntryID = $CommentData["application_entry_id"];
		$this->CommentContent = $CommentData["comment_content"];
		
		if($CommentData["author_member"] == "true"){
			$AccountController = $Controller->UseApps["account"];

			$Account = $AccountController->getUserData($CommentData["author"]);
			
			if($Account == false){
				$this->Author = "user_deleted";
			}
			else {
				$this->Author = $Account->getUsername();
			}
		}
		else {
			$this->Author = $CommentData["author"];
		}
	}

	function getID(){
		return $this->ID;
	}
	
	function getAuthor(){
		return $this->Author;
	}
	
	function getAuthorEmail(){
		return $this->AuthorEmail;
	}
	
	function getAuthorWebsite(){
		return $this->AuthorWebSite;
	}
	
	function getDateTime($format){
		return date($format, $this->DateTime);
	}
	
	function getApplicationOwner(){
		return $this->ApplicationOwner;
	}
	
	function getApplicationEntryID(){
		return $this->ApplicationEntryID;
	}
	
	function getContent(){
		$content = str_replace(array("{", "}"), "", $this->CommentContent);
		
		return $content;
	}
	
	function updateComment($id, $content, $email, $website){
		if(!is_numeric($id)){
			return false;
		}
		$MySQL = $this->Controller->UseApps["mysql"];
		
		$NewContent = escape($content, $MySQL);
		
		if(!isEmail($email)){
			return false;
		}
		
		$NewEmail = $email;
		$NewWebsite = $website;
		
		$MySQL->query("UPDATE {$MySQL->getPrefix()}comment SET comment_content = '$NewContent', author_email = '$NewEmail', author_website = '$NewWebsite' WHERE id = $id");
		
		return true;
	}
}

?>